# ! /usr/bin/env python
# coding=utf-8
'''
@author: fisher

@desc: 作业盒子学生端基本方法

'''


from selenium import webdriver
import time
import os.path
from element import mathElement
from utility import base


class CommonMethond():

    def __init__(self, driver):
        self.driver = driver

    def match_dfillin(self, questionIdListAPI):

        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        img_path = os.path.abspath(os.path.join(os.path.join(__file__, '../log'), '截图'))

        submitted = False

        question_number = 0

        while not submitted:
            question_number += 1
            next_kong = self.driver.find_element_by_xpath(mathElement.Match.next_kong_loc)
            next_btn = self.driver.find_element_by_xpath(mathElement.Match.NextTi_loc)
            submit_btn = self.driver.find_element_by_xpath(mathElement.Match.submit_btn_loc)

            # 纯数字题
            choice_dot = self.driver.find_element_by_xpath(mathElement.Match.choice_dot_loc)
            choice_one = self.driver.find_element_by_xpath(mathElement.Match.choice_one_loc)
            # xyz+-*/和x方键盘
            Subtraction_btn = self.driver.find_element_by_xpath(mathElement.Match.Subtraction_btn_loc)
            Addition_btn = self.driver.find_element_by_xpath(mathElement.Match.Addition_btn_loc)

            novice_guide = self.driver.find_element_by_ios_predicate(mathElement.Match.novice_guide_loc)
            if novice_guide:
                novice_guide.click()
                print '【APP】>>>>作答场景处理了题干中音频的新手引导'

            # 填空题
            # 纯数字题
            if choice_one and choice_dot:
                choice_one.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_kong:
                    next_kong.click()
                    self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + '题填空题,questionId:' + questionIdListAPI[question_number - 1] + '下一空.png')
                elif next_btn:
                    next_btn.click()
                    self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + '题填空题,questionId:' + questionIdListAPI[question_number - 1] + '下一题.png')
            # xyz+-*/和x方键盘
            elif Subtraction_btn and Addition_btn and choice_dot:
                Subtraction_btn.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_kong:
                    next_kong.click()
                    self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + '题填空题,questionId:' + questionIdListAPI[question_number - 1] + '下一空.png')
                elif next_btn:
                    next_btn.click()
                    self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + '题填空题,questionId:' + questionIdListAPI[question_number - 1] + '下一题.png')
            else:
                self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + "题，不是填空题+条件不满足.png")
                self.assertTrue(False, u"第 %d 道题条件不满足" % question_number)



















